import { request, useUserStore, clearEmpty } from "jmash-core";
import type { AxiosPromise } from "axios";
import type {
  OperationApi,
  OperationReq,
  OperationPage,
  OperationCreateReq,
  OperationUpdateReq,
  OperationKey,
  OperationModel,
  OperationKeyList,
  OperationMoveKey,
} from "./types";

class OperationApiImpl implements OperationApi {
  getKey(model: OperationModel): OperationKey {
    return {
      operationId: model.operationId,
    } as OperationKey;
  }

  /**
   * 获取分页信息
   *
   * @param queryParams 分页查询参数，可选
   * @returns 返回请求结果
   */
  findPage(query: OperationReq): AxiosPromise<OperationPage> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/rbac/operation/page",
      method: "get",
      params: query,
    });
  }

  // 查询操作表
  findById(query: OperationKey): AxiosPromise<OperationModel> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/rbac/operation/id",
      method: "get",
      params: query,
    });
  }

  /**
   * 添加操作
   *
   * @param data 操作数据
   * @returns 返回请求结果
   */
  create(data: OperationCreateReq): AxiosPromise<OperationModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/operation",
      method: "post",
      data: data,
    });
  }

  /**
   * 编辑操作
   *
   * @param data 编辑数据
   * @returns 返回请求结果
   */
  update(data: OperationUpdateReq): AxiosPromise<OperationModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/operation",
      method: "patch",
      data: data,
    });
  }

  /**
   * 删除操作
   *
   * @param operationId 操作ID
   * @returns 返回请求结果
   */
  delete(query: OperationKey): AxiosPromise<OperationModel> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/rbac/operation/id",
      method: "delete",
      params: query,
    });
  }

  /**
   * 批量删除操作
   *
   * @param ids 操作ID数组
   * @returns 返回请求结果
   */
  batchDelete(keys: OperationKey[]): AxiosPromise<number> {
    const ids = keys.map((item) => item.operationId);
    const data: OperationKeyList = {
      tenant: useUserStore().user.tenant,
      moduleId: ids,
    } as OperationKeyList;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/operation/batch",
      method: "put",
      data: data,
    });
  }

  /**
   * 向上/下移动模块
   *
   * @param roleId 模块ID
   * @returns 返回Promise对象，表示异步操作的结果
   */
  move(key: OperationKey, up: boolean): AxiosPromise<boolean> {
    const data: OperationMoveKey = { ...key };
    data.tenant = useUserStore().user.tenant;
    data.up = up;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/operation/move",
      method: "put",
      data: data,
    });
  }

  checkOperationApi(operationCode: string): AxiosPromise {
    return request({
      url: "/v1/rbac/operation/check",
      method: "get",
      params: {
        tenant: useUserStore().user.tenant,
        operationCode: operationCode,
      },
    });
  }
}

const operationApi = new OperationApiImpl();
export { operationApi };
